package com.gla.market.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.gla.market.PetShopApplication;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Callback;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 基础网络请求工具类
 *
 */
public class NetUtil {
    /**
     * 单例化http客户端
     */
    private static OkHttpClient mClient = new OkHttpClient();
    //设置超时时间
    private static final OkHttpClient CLIENT = new OkHttpClient.Builder()
            .connectTimeout(20, TimeUnit.SECONDS)
            .writeTimeout(20,TimeUnit.SECONDS)
            .readTimeout(20,TimeUnit.SECONDS)
            .build();

    /**
     * 向指定地址发送get请求
     *
     * @param url      目标地址
     * @param callback 自定义回调函数
     */
    public static void doGet(String url, Callback callback) {
        Log.d("GET Url", url);

        Context context = PetShopApplication.mContext;
        Request request = new Request.Builder().url(url).get()
                .addHeader("Authorization",(String) SharedPreferencesUtil.get(context,"userInfo","token","null"))
                .build();
        mClient.newCall(request).enqueue(callback);
    }

    /**
     * 向指定地址发送post请求
     *
     * @param url      目标地址
     * @param body     请求体
     * @param callback 自定义回调函数
     */
    public static void doPost(String url, RequestBody body, Callback callback) {
        Log.d("POST Url", url);
        Context context = PetShopApplication.mContext;
        Request request = new Request.Builder().url(url).post(body).addHeader("Authorization",(String) SharedPreferencesUtil.get(context,"userInfo","token","null"))
                .build();
        mClient.newCall(request).enqueue(callback);
    }

    /**
     * 检测网络是否可用
     *
     * @param context content
     * @return true/false  可用/不可用
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                // 当前网络是连接的
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    // 当前所连接的网络可用
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * 图片上传
     */
    public static void upload(String url,String filePath,Callback callback){
        java.io.File file = new File(filePath);//创建文件
        okhttp3.RequestBody formBody = new MultipartBody.Builder()//转为二进制
                .setType(MultipartBody.FORM)
                .addFormDataPart("userpic",
                        file.getName(),
                        okhttp3.RequestBody.create(okhttp3.MediaType.parse("application/octet-stream"),file))
                .build();
        okhttp3.Request request = new Request.Builder().url(url).post(formBody).build();
        CLIENT.newCall(request).enqueue(callback);
    }
}
